package com.ruoyi.web.controller.system;

import java.text.SimpleDateFormat;
import java.util.List;

import org.apache.ibatis.annotations.Param;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysAttendance;
import com.ruoyi.system.service.ISysAttendanceService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.annotation.security.PermitAll;

/**
 * 考勤Controller
 * 
 * @author ruoyi
 * @date 2023-01-04
 */
@RestController
@RequestMapping("/system/attendance")
public class SysAttendanceController extends BaseController
{
    @Autowired
    private ISysAttendanceService sysAttendanceService;

    /**
     * 查询考勤列表
     */

    @PreAuthorize("@ss.hasPermi('system:attendance:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysAttendance sysAttendance)
    {
        startPage();
        List<SysAttendance> list = sysAttendanceService.selectSysAttendanceList(sysAttendance);
        return getDataTable(list);
    }

    /**
     * 导出考勤列表
     */
    @PreAuthorize("@ss.hasPermi('system:attendance:export')")
    @Log(title = "考勤", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(SysAttendance sysAttendance)
    {
        List<SysAttendance> list = sysAttendanceService.selectSysAttendanceList(sysAttendance);
        ExcelUtil<SysAttendance> util = new ExcelUtil<SysAttendance>(SysAttendance.class);
        return util.exportExcel(list, "attendance");
    }

    /**
     * 获取考勤详细信息
     */
    //@PreAuthorize("@ss.hasPermi('system:attendance:query')")
    @PermitAll
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(sysAttendanceService.selectSysAttendanceById(id));
    }

    /**
     * 新增考勤
     */

    @Log(title = "考勤", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysAttendance sysAttendance)
    {
        System.out.println(sysAttendance);
        return toAjax(sysAttendanceService.saveSysAttendance(sysAttendance));
    }

    /**
     * 修改考勤
     */
    @PreAuthorize("@ss.hasPermi('system:attendance:edit')")
    @Log(title = "考勤", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysAttendance sysAttendance)
    {
        return toAjax(sysAttendanceService.updateSysAttendance(sysAttendance));
    }

    /**
     * 删除考勤
     */
    @PreAuthorize("@ss.hasPermi('system:attendance:remove')")
    @Log(title = "考勤", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(sysAttendanceService.deleteSysAttendanceByIds(ids));
    }

    /**
     * 按月查询
     */


    @GetMapping("/month/{month}")
    public TableDataInfo month(@PathVariable String month)
    {


        startPage();
        List<SysAttendance> list=sysAttendanceService.selectSysAttendanceByMonth(month);
        return getDataTable(list);
    }

    /**
     * 按班组ID查询
     */

    @GetMapping("/class")
    public TableDataInfo classID (@Param("classID") String classID, @Param("month") String month)
    {
        SimpleDateFormat formatter;
        formatter = new SimpleDateFormat ("dd");
        startPage();
        List<SysAttendance> list=sysAttendanceService.selectSysAttendanceByClass(classID,month);
        for (SysAttendance attendance: list
             ) {
            attendance.setDay(Integer.parseInt(formatter.format(attendance.getDate())));}
        return getDataTable(list);
    }

    @PostMapping("/saveAttendances")
    public AjaxResult save(@RequestBody List<SysAttendance> sysAttendances)
    {
        for (SysAttendance attendance:sysAttendances
             ) {

            sysAttendanceService.saveSysAttendance(attendance);
        }
        return AjaxResult.success();
    }





}
